package com.dmx.adi;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dmx.adi.AdiRemoteFileExtractor;
import com.dmx.net.ConnectionException;
import com.dmx.net.HostInfoExtractor;
import com.dmx.net.ftp.impl.FtpClient;
import com.dmx.net.ftp.impl.RemoteFtpFileNotExistsException;
import com.dmx.net.transfer.impl.TransferCommand;

public class AdiRemotePackageValidator {

	private FtpClient ftpClient;

	private URL remoteAdiXml;

	private File localAdiXml;

	private List<URL> lstGoodContent = new ArrayList<URL>();

	private List<URL> lstBadContent = new ArrayList<URL>();

	private boolean adiFileRetrived = true;

	private boolean validateOk = false;

	public static Logger log = LoggerFactory
			.getLogger(AdiRemotePackageValidator.class);

	protected AdiRemotePackageValidator(URL remoteAdiUrl, File localAdiXml) {
		this.localAdiXml = localAdiXml;
		this.remoteAdiXml = remoteAdiUrl;
		this.ftpClient = new FtpClient();
	}

	public static AdiRemotePackageValidator fromRemote(URL remoteAdiUrl,
			File tempFolder) {
		File localAdiXml = new File(tempFolder, UUID.randomUUID().toString()
				+ ".xml");
		AdiRemotePackageValidator ret = new AdiRemotePackageValidator(
				remoteAdiUrl, localAdiXml);

		ret.adiFileRetrived = false;

		return ret;
	}

	public static AdiRemotePackageValidator fromLocalAdiFile(URL remoteAdiUrl,
			File localAdiXml) {
		AdiRemotePackageValidator ret = new AdiRemotePackageValidator(
				remoteAdiUrl, localAdiXml);

		ret.adiFileRetrived = true;

		return ret;
	}

	public void validate() throws ConnectionException,
			RemoteFtpFileNotExistsException, Exception {

		try {
			try {
				if (adiFileRetrived == false) {
					TransferCommand tCommand = new TransferCommand(
							new FtpClient());
					tCommand.getFile(localAdiXml, this.remoteAdiXml);

				}
			} catch (ConnectionException ce) {
				throw ce;
			} catch (RemoteFtpFileNotExistsException rfne) {
				throw rfne;
			}

			HostInfoExtractor hinfo = new HostInfoExtractor(this.remoteAdiXml);
			hinfo.decorateNetworkProtocolClient(this.ftpClient);
			ftpClient.safeLogin();

			AdiRemoteFileExtractor adiRemoteFileExtractor = new AdiRemoteFileExtractor(
					hinfo.extractTheFolderFullUrl(), this.localAdiXml);

			List<URL> lstUrl = adiRemoteFileExtractor.extractFiles();

			for (URL url : lstUrl) {
				try {
					ftpClient.testRemoteFileExists(url.getFile());
					lstGoodContent.add(url);
				} catch (RemoteFtpFileNotExistsException rfe) {
					lstBadContent.add(url);

				}
			}

			if (lstBadContent.isEmpty() == true) {
				validateOk = true;
			} else {
				validateOk = false;
			}

		} finally {
			ftpClient.close();

			if (adiFileRetrived == false) {
				// this means the file is generated by validator, it will take
				// care of it
				if (this.localAdiXml.isFile()) {
					this.localAdiXml.delete();
				}
			}
		}

	}

	public List<URL> getLstGoodContent() {
		return lstGoodContent;
	}

	public List<URL> getLstBadContent() {
		return lstBadContent;
	}

	public boolean isValidateOk() {
		return validateOk;
	}

}
